package com.wushuu.ce.db;

import android.util.Log;

import com.wushuu.ce.utils.ResourceUtils;

import org.xutils.DbManager;
import org.xutils.DbManager.DaoConfig;
import org.xutils.common.util.LogUtil;
import org.xutils.x;

import java.io.File;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * ============================================================
 * <br/>
 * 作 者 :        xyb
 * <br/>
 * 版 本 :        1.0
 * <br/>
 * 创建日期 ：     2016-05-13 下午5:39
 * <br/>
 * 描 述 ：
 * <br/>
 * 修订历史 ：
 * <br/>
 * ============================================================
 **/
public class BaseDbHelper {

    protected static DbManager mDbManager;
    private static final String DB_NAME = "ce.db";

    static {
        DaoConfig daoConfig = new DaoConfig();
        daoConfig.setDbName(DB_NAME).setDbDir(new
                File(ResourceUtils.getContext().getFilesDir().getAbsolutePath())).setDbVersion(1).setDbUpgradeListener(new DbManager.DbUpgradeListener() {
            @Override
            public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                LogUtil.d(DB_NAME + " upgrade from " + oldVersion + "--->" + newVersion);
            }
        });
        mDbManager = x.getDb(daoConfig);
    }

    private static int dbThreadCount = 3;//db线程池数量

    private static final ThreadFactory sThreadFactory = new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);

        public Thread newThread(Runnable r) {
            Thread tread = new Thread(r, "db #" + mCount.getAndIncrement());
            tread.setPriority(Thread.NORM_PRIORITY - 1);
            return tread;
        }
    };

    protected static final Executor executor = Executors.newFixedThreadPool(dbThreadCount, sThreadFactory);

}
